#include "BondDetailTools.h"
#include "../BondUtility.h"
#include "../../bondlib/SSVContainer.h"
#include <bondlib/RealTimeDeal/BondMarketStreamV2.h>
#include <core/time/system_time.h>

bool BondDetailTools::IsValidateTime(time_t tmTime)
{
	if (tmTime > 0 && (int)(qb::base::IntDate::timeToInt(tmTime)) == CMarketStream::GetBondDealInvalidateDay())	return true;
	return false;
}

void BondDetailTools::ConvFromMktStmInfo(const MarketStreamInfo& markInfo, xQBMarketStreamUnit_c& unitInfo)
{
	FIELDCOPY(unitInfo.m_company_id, markInfo.m_company_id);
	//FIELDCOPY(unitInfo.m_company_name,markInfo.m_company_name);
	FIELDCOPY(unitInfo.m_dept, markInfo.m_dept);
	FIELDCOPY(unitInfo.m_type, markInfo.m_type);
	FIELDCOPY(unitInfo.m_operate, markInfo.m_operate);
	FIELDCOPY(unitInfo.m_status, markInfo.m_status);
	unitInfo.m_modify_time = markInfo.m_modify_time;
	unitInfo.m_create_time = markInfo.m_create_time;
	unitInfo.m_indexID = markInfo.m_indexID;
	//FIELDCOPY(unitInfo.m_body2,markInfo.m_body);

	unitInfo.m_body2.m_createTime = markInfo.m_body.m_createTime;
	//FIELDCOPY(unitInfo.m_body2.m_bondCode,markInfo.m_body.m_bondCode);
	FIELDCOPY(unitInfo.m_body2.m_bondkey, markInfo.m_body.m_bondkey);
	FIELDCOPY(unitInfo.m_body2.m_listedmarket, markInfo.m_body.m_listedmarket);
	//FIELDCOPY(unitInfo.m_body2.m_bondShortName,markInfo.m_body.m_bondShortName);
	FIELDCOPY(unitInfo.m_body2.m_price, markInfo.m_body.m_price);
	FIELDCOPY(unitInfo.m_body2.m_volume, markInfo.m_body.m_volume);
	FIELDCOPY(unitInfo.m_body2.m_fullPrice, markInfo.m_body.m_fullPrice);
	FIELDCOPY(unitInfo.m_body2.m_cleanPrice, markInfo.m_body.m_cleanPrice);
	FIELDCOPY(unitInfo.m_body2.m_yield, markInfo.m_body.m_yield);
	FIELDCOPY(unitInfo.m_body2.m_rebate, markInfo.m_body.m_rebate);
	FIELDCOPY(unitInfo.m_body2.m_return_point, markInfo.m_body.m_return_point);
	FIELDCOPY(unitInfo.m_body2.m_id, markInfo.m_body.m_id);
	FIELDCOPY(unitInfo.m_body2.m_dealStatus, markInfo.m_body.m_dealStatus);
	FIELDCOPY(unitInfo.m_body2.m_exercise, markInfo.m_body.m_exercise);
	FIELDCOPY(unitInfo.m_body2.m_quotetype, markInfo.m_body.m_quotetype);
}

QString BondDetailTools::GetBidAreaFormat(const CBondInfo& info)
{
	QString strReturn = "-- - --";
	if (info.IsValidBond()) {
		QString strBott = "";
		QString strTop = "";;
		if (info.IsRatingDebt())
		{
			if (strlen(info.GetAuctionFlr()) > 0 && strlen(info.GetAuctionCap()) > 0) {
				strBott = QString("%1").arg(info.GetAuctionFlr());
				strTop = QString("%1").arg(info.GetAuctionCap());
			}
		}
		else
		{
			if (strlen(info.GetBidLimitBottom()) > 0 && strlen(info.GetBidLimitTop()) > 0) {
				strBott = QString("%1").arg(info.GetBidLimitBottom());
				strTop = QString("%1").arg(info.GetBidLimitTop());
			}
		}

		if (!strBott.isEmpty() && !strTop.isEmpty()) {
			QString strBott1 = BondUtility::FormatBidLimitTop(strBott);
			QString strTop1 = BondUtility::FormatBidLimitTop(strTop);
			strReturn = QString(("%1 - %2")).arg(strBott1).arg(strTop1);
		}
	}
	return strReturn;
}

void BondDetailTools::FormatBussinessShowInfo(CBusinessShowInfo& info)
{
	std::vector<CShowUnitInfo> tempList = info.m_AllowList;
	info.m_AllowList.clear();
	std::string strHongYuanId = "402880f034219aed0134219e10b00727";//默认宏源排在前面
	for (auto itr = tempList.begin(); itr != tempList.end(); ++itr) {
		if (itr->m_BrokerID == strHongYuanId) {
			info.m_AllowList.push_back(*itr);
			break;
		}
	}
	for (auto itr = tempList.begin(); itr != tempList.end(); ++itr) {
		if (itr->m_BrokerID != strHongYuanId) {
			info.m_AllowList.push_back(*itr);
		}
	}
}

bool BondDetailTools::getEnablePucharceList(QString combondKey, CBusinessShowInfo& info)
{
	if (combondKey.isEmpty())return false;
	int bondIndex = CBondContainer::instance().GetBondIndex(combondKey.toStdString().c_str());
	if (!CBondContainer::instance().IsValidIndex(bondIndex))return false;

	const CBondInfo& bondInfo = CBondContainer::instance().ElementAtR(bondIndex);
	if (strcmp(bondInfo.GetMarketType(), "1") != 0)
		return false;

	if (SSUserAccountManager::instance().GetPrimaryMarketAuth_All(info))
	{
		std::vector<CShowUnitInfo> tempList = info.m_AllowList;
		info.m_AllowList.clear();
		int nSz = tempList.size();
		for (int i = 0; i < nSz; i++)
		{ //TODO:HY数据类
			//HY_BOND_QUOTATION_INFO hyqi;
			//if (AfxGetHYQuotationMap(tempList[i].m_BrokerID.c_str()).GetBondQuotation(bondInfo.GetBondKey(), bondInfo.GetListedMarket(), hyqi))
			//{
			//	if (hyqi.m_Status != 0 && hyqi.m_Status != 4)
			//		continue;
			//	if (hyqi.m_Pushed == 1 && hyqi.m_DelayPublication != 1)
			//	{
			//		//推送方式，0：全量；1：白名单；2：黑名单（排除模式）
			//		bool bHave = AfxGetHYQuotationMap(tempList[i].m_BrokerID.c_str()).CheckInList(bondInfo.GetBondKey()
			//			, bondInfo.GetListedMarket(), SSUserAccountManager::instance().GetUserInfo().m_CompanyId);
			//		if (bHave)
			//		{
			//			time_t tEndTime = CNewDetailTools::GetValidEndTime(hyqi.m_BidEndTime, bondInfo);
			//			if (tEndTime > CSSVContainer::GetServerTime())
			//			{
			//				info.m_AllowList.push_back(tempList[i]);
			//			}
			//		}
			//	}
			//}
		}
		if (info.m_AllowList.size() > 0)
		{
			FormatBussinessShowInfo(info);
			return true;
		}

	}
	return false;
}

